package com.my.cachedemo.service;

import com.my.cachedemo.bean.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * Caffeine cache
 *
 * @author 王昊然
 * @create 2018-12-28 18:31
 **/
@Slf4j
@Service
public class CacheService {

    @Cacheable(value = "user1", key = "#id", sync = true)
    public User getUser(int id){

        User user = new User();
        user.setId(id);
        user.setName("张三" + RandomStringUtils.random(2));
        log.info("创建了新用户");

        return user;
    }

}
